/*
 * @(#)AdlText.java   09/10/11
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.gea.spec;

import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;

import fr.ema.arch3d.model.architecture.Behavior;
import fr.ema.arch3d.model.architecture.specification.ComponentRole;
import fr.ema.arch3d.model.architecture.specification.Connection;
import fr.ema.arch3d.model.architecture.specification.Specification;
import fr.ema.arch3d.model.architecture.textADL.ADLDoc;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ADLTextSpec  {


    /**
     * Generates the <code>ADLDoc</code> for <code>Specification</code>
     *
     * @param spec  the <code>Specification</code> to be generated ADLDoc
     * @return  the <code>ADLDoc</code> of <code>Specification</code>
     */
    public static Document createTextSpec(ADLDoc doc, Specification spec) {
        // describe specification name
        doc.insertBold("specifction");
        doc.insertSpace(1);
        doc.insert(spec.getName());
        doc.changeLine();

        // describe component types
        doc.insertSpace(2);
        doc.insertBold("component_roles");
        doc.changeLine();
        doc.insertSpace(2);
        for (int i = 0; i < spec.getCmpRoleList().size(); i++) {
            doc.insertSpace(1);
            doc.insertSpace(1);
            doc.insert(((ComponentRole) spec.getCmpRoleList().get(i)).getName() + ";");
            if ((i > 1) && (i + 1) % 3 == 0) {
                doc.changeLine();
            }
        }


        // describe connections
        doc.insertSpace(2);
        doc.insertBold("connections");
        doc.changeLine();

        for (int i = 0; i < spec.getConnectionList().size(); i++) {
            doc.insertSpace(4);
            doc.insertBold("connection");
            doc.insertSpace(1);
            doc.insert(((Connection) spec.getConnectionList().get(i)).getName());
            doc.changeLine();
            doc.insertSpace(6);
            doc.insertBold("client");
            doc.insertSpace(1);
            doc.insert(((Connection) spec.getConnectionList().get(i)).getClientCmpRole().getName());
            doc.insert(".");
            doc.insert(((Connection) spec.getConnectionList().get(i)).getItfClientName());
            doc.changeLine();
            doc.insertSpace(6);
            doc.insertBold("server");
            doc.insertSpace(1);
            doc.insert(((Connection) spec.getConnectionList().get(i)).getItfServerName());
            doc.insert(".");
            doc.insert(((Connection) spec.getConnectionList().get(i)).getItfServerName());
            doc.changeLine();
            doc.insertSpace(4);
            doc.insertBold("end");
            doc.insertSpace(1);
            doc.insert(((Connection) spec.getConnectionList().get(i)).getName());
            doc.changeLine();
        }

        // describe architecture behaviors
        doc.insertSpace(2);
        doc.insertBold("architecture_behavior");

        // doc.changeLine();
        for (int i = 0; i < spec.getArchBehList().size(); i++) {
            doc.insertSpace(4);
            doc.insert(((Behavior) spec.getArchBehList().get(i)).getBehavior() + ";");
            doc.changeLine();
        }
        doc.changeLine();
        // end describe specification
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(spec.getName());
        doc.changeLine();

        return doc;
    }

    /**
     * Generates the <code>ADLDoc</code> for <code>ComponentRole</code>
     *
     * @param  cr  the <code>ComponentRole</code> to be generated ADLDoc
     * @return  the <code>ADLDoc</code> of <code>ComponentRole</code>
     */
    public static Document createTextCmp(ADLDoc doc, ComponentRole cr) {

        // describe component type
        doc.insertBold("component role");
        doc.insertSpace(1);
        doc.insert(cr.getName());
        doc.changeLine();

        // describe component required interfaces
        int j = 0;

        for (int i = 0; i < cr.getInterfaces().length; i++) {
            if (cr.getInterfaces()[i].isFcClientItf()) {
                if (j == 0) {
                    doc.insertSpace(1);
                    doc.insertBold("interface required");
                    doc.changeLine();
                    doc.insertSpace(1);
                    doc.insertSpace(1);
                }

                doc.insert(cr.getInterfaces()[i].getFcItfName());
                doc.insertSpace(1);
                j++;
            }
        }

        if (j != 0) {
            doc.changeLine();
        }

        // describe provided interfaces
        int jj = 0;

        for (int i = 0; i < cr.getInterfaces().length; i++) {
            if (!cr.getInterfaces()[i].isFcClientItf()) {
                if (jj == 0) {
                    doc.insertSpace(1);
                    doc.insertBold("interface provided");
                    doc.changeLine();
                    doc.insertSpace(1);
                    doc.insertSpace(1);
                }

                doc.insert(cr.getInterfaces()[i].getFcItfName());
                doc.insertSpace(1);
                jj++;
            }
        }

        if (jj != 0) {
            doc.changeLine();
        }

        // describe component behavior
        doc.insertSpace(1);
        doc.insertBold("component behavior");
        doc.changeLine();

        // end description
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(cr.getName());

        return doc;
    }


    /**
     * Generates the <code>ADLDoc</code> for <code>Connection</code>
     * @param cnt  the <code>ComponentRole</code> to be generated ADLDoc
     * @return  the <code>ADLDoc</code> of <code>ComponentRole</code>
     */
    public static Document createTextCnt(ADLDoc doc, Connection cnt) {

        // describe component type
        doc.insertBold("connectioin");
        doc.insertSpace(1);
        doc.insert(cnt.getName());
        doc.changeLine();

        // describe required component which it connects
        doc.insertSpace(1);
        doc.insertBold("required_component");
        doc.insertSpace(1);
        doc.insert(cnt.getClientCmpRole().getName());
        doc.changeLine();

        // describe required interface name
        doc.insertSpace(1);
        doc.insertBold("required_interface");
        doc.insertSpace(1);
        doc.insert(cnt.getItfClientName());
        doc.changeLine();

        // describe provided component which it connects
        doc.insertSpace(1);
        doc.insertBold("provided_component");
        doc.insertSpace(1);
        doc.insert(cnt.getServerCompRole().getName());
        doc.changeLine();

        // describe provided interfaces
        doc.insertSpace(1);
        doc.insertBold("interface provided");
        doc.insertSpace(1);
        doc.insert(cnt.getItfServerName());
        doc.changeLine();

        // end description
        doc.insertBold("end");
        doc.insertSpace(1);
        doc.insert(cnt.getName());

        return doc;
    }
}

